<template>
  <div class="">
    <header>
      <input type="text" v-model="name"/> <button @click="add">添加</button> <button @click="sortArr">排序</button>
    </header>
    <ul>
        <li v-for="(item,index) in list" :key="index">
            <span>{{item.name}}</span>
            <span>{{item.time}}</span>
        </li>
    </ul>
  </div>
</template>

<script>
import moment from 'moment';
export default {
  data() {
    return {
      name:'',
      list:[{name:'张三',time:'2021-10-19 16:15:00'}],
      issort: true, // true 代表升序
    };
  },
  mounted() {

  },
  methods: {
    add(){
       let obj = {name:this.name,time:moment(new Date()).format('YYYY-MM-DD HH:mm:ss')}
       this.list.push(obj);
    },
    sortArr(){
      this.issort = !this.issort;
      console.log(+new Date('2021-10-19 16:15:00'));
      if (this.issort){
          this.list.sort((a,b)=> +new Date(a.time) - +new Date(b.time))
      } else {
          this.list.sort((a,b)=> +new Date(b.time) - +new Date(a.time))
      }
    }
  },
  components: {},
};
</script>

<style lang='scss'>
</style>
